import type { Linter } from 'eslint'
import type { FlatGitignoreOptions } from 'eslint-config-flat-gitignore'
import type { Options as VueBlocksOption } from 'eslint-processor-vue-blocks'
import type { ConfigNames, RuleOptions as Rules } from './typegen'

/**
 * 带类型的规则集合
 */
export type { Rules, ConfigNames }

/**
 * 规则带类型提示的 flat config 配置
 */
export interface FlatConfig extends Omit<Linter.Config<Linter.RulesRecord & Rules>, 'plugins'> {
  /**
   * 为 `eslint-flat-config-utils#composer` 提供名称
   */
  name?: string
  /**
   * 为避免大部分 eslint plugin 无 ts 声明文件，或 eslint plugin 依赖的 `@types/eslint` 版本于当前不同导致的类型错误
   * 直接讲插件的类型重置为 any
   */
  plugins?: Record<string, any>
  rules?: Linter.RulesRecord & Rules
}

export interface OptionsOverrides {
  overrides?: FlatConfig['rules']
}

export interface CommonOptions {
  /**
   * [eslint-config-flat-gitignore](https://github.com/antfu/eslint-config-flat-gitignore) 规则配置
   */
  gitignore?: FlatGitignoreOptions
  /**
   * [eslint](https://eslint.org/docs/latest/rules/) 核心规则配置
   */
  javascript?: OptionsOverrides

  /**
   * [eslint-plugin-n](https://github.com/eslint-community/eslint-plugin-n/blob/master/README.md#-rules) 规则配置
   */
  n?: OptionsOverrides
  /**
   * [eslint-plugin-unicon](https://github.com/sindresorhus/eslint-plugin-unicorn?tab=readme-ov-file#rules) 规则配置
   */
  unicon?: OptionsOverrides
  /**
   * [eslint-plugin-import](https://github.com/import-js/eslint-plugin-import?tab=readme-ov-file#rules) 规则配置
   */
  imports?: OptionsOverrides
}

export interface VueOptions extends OptionsOverrides {
  /**
   * 当前 vue 版本号
   *
   * 默认自动根据安装的 vue 版本判断
   */
  version?: 2 | 3
  /**
   * 是否使用 [eslint-processor-vue-blocks](https://github.com/antfu/eslint-processor-vue-blocks) 将 vue 文件代码块拆分至单独虚拟文件lint
   * @default true
   */
  vueBlocks?: boolean | VueBlocksOption
}

/**
 * 规则配置
 */
export interface Options extends CommonOptions {
  /**
   * [@typescript-eslint/eslint-plugin](https://typescript-eslint.io/rules/) 规则配置
   */
  typescript?: boolean | OptionsOverrides
  /**
   * [eslint-plugin-vue](https://eslint.vuejs.org/rules/) 规则配置
   */
  vue?: boolean | VueOptions

  /**
   * prettier 默认会关闭和它冲突的规则，这个配置项，使你可以覆盖这些配置项
   */
  prettier?: boolean | OptionsOverrides
  /**
   * [eslint-plugin-command](https://eslint-plugin-command.antfu.me/)
   */
  command?: boolean | OptionsOverrides
}

type _RequiredOptions = Required<Options>

export type _EnableOptions<T> = {
  [P in keyof T]: Exclude<T[P], boolean> & { enable: boolean }
}

type _ResolveOptions = CommonOptions & _EnableOptions<Omit<_RequiredOptions, keyof CommonOptions>>

export interface ResolveOptions extends _ResolveOptions {}
